package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.back.bizactions;

import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.svc.document.storage.api.DocStorageService;
import com.inspur.edp.svc.document.storage.entity.GspDocOperatingModes;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class sbyjbyfkdeleteFjMgrAction extends AbstractManagerAction<String> {
    private String metadataId;
    private String rootId;
    private String mxid;
    private String fjid;

    public sbyjbyfkdeleteFjMgrAction(IBEManagerContext managerContext, String metadataId, String rootId, String mxid, String fjid) {
        super(managerContext);
        this.metadataId = metadataId;
        this.rootId = rootId;
        this.mxid = mxid;
        this.fjid = fjid;
    }

    private String ybmxCfg = "com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.back.newybfkmx";

    @Override
    public void execute() {
        String rootId = "maintenance";
        RpcClassHolder rpcClassHolder = SpringBeanUtils.getBean(RpcClassHolder.class);
        DocStorageService docStorageService = rpcClassHolder.getRpcClass(DocStorageService.class);
        try {
            SpringBeanUtils.getBean(IBefSessionManager.class).createSession(Duration.ofMinutes(5));
            IStandardLcp mxLcp = LcpUtil.getLcp(ybmxCfg);
            //获取一保明细数据
            IEntityData data = mxLcp.retrieve(mxid).getData();
            ArrayList<String> nodeCodes = new ArrayList<>();
            nodeCodes.add("fjInfo");
            ArrayList<String> hierarchyIds = new ArrayList<>();
            hierarchyIds.add(mxid);
            List<String> deleteIds = new ArrayList<>();
            deleteIds.add(fjid);
            //先删除一保明细下的附件子表数据
            mxLcp.deleteChild(nodeCodes, hierarchyIds, deleteIds);
            mxLcp.save();
            //后删除文件系统中的文件信息
            boolean removeDoc = docStorageService.removeDoc(metadataId, GspDocOperatingModes.Formal, rootId);
            setResult("删除文件" + (removeDoc ? "成功" : "失败"));

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }


    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}